package android_dhbw.gpx;

import java.util.List;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.PathOverlay;
import de.dhbw.GpsTools.AdvancedTrack;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class TrackOnMapActivity extends Activity implements LocationListener{
	
	private SharedPreferences prefs;
	
	private Button btn_center;
	private Button btn_firstWpt;
	
	private MapView mapView;
	private MapController mapController;
	private PathOverlay trackOverlay;
	private MyLocationOverlay myLocation;
	private List<Overlay> mapOverlays;
	private LocationManager locManager;
	
	private AdvancedTrack globalTrack;
	
	private boolean setCenter = false;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tracklayout);

	    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0,  this);
        
        btn_center = (Button) this.findViewById(R.id.track_btn_centerPosition);
        btn_center.setOnClickListener(showPositionListener);
        
        btn_firstWpt = (Button) this.findViewById(R.id.track_btn_centerFirstWpt);
        btn_firstWpt.setOnClickListener(showFirstWptListener);
        
        mapView = (MapView) this.findViewById(R.id.mapview);
        //mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);
        mapView.setClickable(true);
        mapView.setOnTouchListener(mapClickedListener);
        
        prefs = this.getSharedPreferences("android_dhbw.gpx", Context.MODE_PRIVATE);
        
        switch (prefs.getInt("SETTINGS_SPINNER_POSITION", 0)){
        case 0:
        	mapView.setTileSource(TileSourceFactory.MAPNIK);
        	break;
        case 1:
        	mapView.setTileSource(TileSourceFactory.CYCLEMAP);
        }
        mapController = mapView.getController();
        
        mapOverlays = mapView.getOverlays();
        
        myLocation = new MyLocationOverlay(this, mapView);
        mapOverlays.add(myLocation);
        
        trackOverlay = new PathOverlay(Color.RED, this);
        mapOverlays.add(trackOverlay);
        
        mapController.setZoom(12);
        mapController.setCenter(new GeoPoint(48700000, 9200000));
        
        globalTrack = ((Globals) this.getApplication()).gpxTrack;
        
        if (globalTrack != null)
        {
        	trackOverlay.clearPath();
        	mapController.setCenter(new GeoPoint(new Double(globalTrack.get(0).getLatitude() * 1000000).intValue(), new Double(globalTrack.get(0).getLongitude() * 1000000).intValue()));
	        for (int i = 0; i <  globalTrack.size(); i++)
	        {
	        	trackOverlay.addPoint(new Double(globalTrack.get(i).getLatitude() * 1000000).intValue(), new Double(globalTrack.get(i).getLongitude() * 1000000).intValue());
	        }
        }
        
        
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	myLocation.enableMyLocation();
    	
        switch (prefs.getInt("SETTINGS_SPINNER_POSITION", 0)){
        case 0:
        	mapView.setTileSource(TileSourceFactory.MAPNIK);
        	break;
        case 1:
        	mapView.setTileSource(TileSourceFactory.CYCLEMAP);
        }
    	
        if (globalTrack != ((Globals) this.getApplication()).gpxTrack)
        {
        	globalTrack = ((Globals) this.getApplication()).gpxTrack;
        	
        	trackOverlay.clearPath();
        	mapController.setCenter(new GeoPoint(new Double(globalTrack.get(0).getLatitude() * 1000000).intValue(), new Double(globalTrack.get(0).getLongitude() * 1000000).intValue()));
       
	        for (int i = 0; i <  globalTrack.size(); i++)
	        {
	        	trackOverlay.addPoint(new Double(globalTrack.get(i).getLatitude() * 1000000).intValue(), new Double(globalTrack.get(i).getLongitude() * 1000000).intValue());
	        }
	      
        } 
    }
    
    @Override
    protected void onStop() {
 	   super.onPause();
 	   locManager.removeUpdates(this);
 	   myLocation.disableMyLocation();
    }
    
	private OnClickListener showPositionListener = new OnClickListener(){
		public void onClick(View v){
			if (setCenter == false && myLocation.getMyLocation() != null)
			{
				mapController.animateTo(myLocation.getMyLocation());
				setCenter = true;
			}
		}
	};
	
	private OnClickListener showFirstWptListener = new OnClickListener(){
		public void onClick(View v){
			if (globalTrack != null)
			{
				setCenter = false;
				mapController.animateTo(new GeoPoint(new Double(globalTrack.get(0).getLatitude() * 1000000).intValue(), new Double(globalTrack.get(0).getLongitude() * 1000000).intValue()));
			}
		}
	};
	
	private OnTouchListener mapClickedListener = new OnTouchListener(){
		public boolean onTouch(View v, MotionEvent event) {
				setCenter = false;
			return false;
		}
	};

	public void onLocationChanged(Location location) {
		Double latitude;
		Double longitude;
		
		if (setCenter == true)
		{
			latitude = location.getLatitude() * 1000000;
			longitude = location.getLongitude() * 1000000;
			
			mapController.setCenter(new GeoPoint(latitude.intValue(), longitude.intValue()));
			myLocation.setEnabled(true);
		}
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}
